package com.cenxi.controller;

import com.cenxi.common.aspect.annotation.OpaLog;
import com.cenxi.common.dto.MenuDTO;
import com.cenxi.common.query.MenuQuery;
import com.cenxi.common.result.Req;
import com.cenxi.common.result.Resp;
import com.cenxi.common.vo.MenuVO;
import com.cenxi.manage.ManageMenuService;
import com.cenxi.utils.constants.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author cenxi
 * @Date 2023/11/14 14:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口")
public class MenuController {
    @Autowired
    private ManageMenuService manageMenuService;

    @OpaLog("查看菜单树数据")
    @PostMapping("/treeList")
    @ApiOperation(value = "获取菜单树")
    public Resp<List<MenuVO>> treeList(@RequestBody Req<MenuQuery> req) {
        return Resp.succeed(manageMenuService.treeList(req));
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建菜单权限")
    public Resp<Boolean> create(@RequestBody Req<MenuDTO> req) {
        return manageMenuService.create(req);
    }

    @PostMapping("/findById")
    @ApiOperation(value = "根据id查询平台菜单权限信息")
    public Resp<MenuVO> findById(@RequestBody Req<Long> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.findById(req.getData());
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改菜单权限")
    public Resp<Boolean> edit(@RequestBody Req<MenuDTO> req) {
        return manageMenuService.edit(req);
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用菜单权限")
    public Resp<Boolean> enable(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.enable(req.getData(), Constants.ENABLE);
    }

    @PostMapping("/disable")
    @ApiOperation(value = "禁用菜单权限")
    public Resp<Boolean> disable(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.disable(req.getData(), Constants.DISABLE);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "根据id删除平台菜单权限信息")
    public Resp<Boolean> deleteById(@RequestBody Req<List<Long>> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageMenuService.deleteById(req.getData());
    }
}
